<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
cfc('core')->checklogin('jy_quan/myquan/');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
//检查创建朋友圈
$this->add_member(0,intval($_RQ['formuser']));
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$return['likesnum']=pdo_getsum('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),'likes');
	$return['looksnum']=pdo_getsum('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),'looks');
	$return['jielinum']=pdo_count('jy_quan_item_jieli',array('uniacid'=>$_SESSION['uniacid']),'id');
	$return['fansnum']=pdo_count('jy_quan_userlog',array('uniacid'=>$_SESSION['uniacid'],'isfollow'=>1,'userid'=>$_SESSION['uid']));
	$return['quaninfo']=pdo_get('jy_quan_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	//是否关注
	$return['isfollow']=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('createtime','pcd','credit1','credit2','mobile','avatar','nickname'));
	$return['quaninfo']['title'] =$return['quaninfo']['title']?$return['quaninfo']['title']:$member['nickname'].'的圈';
	$return['quaninfo']['logo'] = tomedia($member['avatar']);
	$return['quaninfo']['pcd'] = $return['quaninfo']['pcd']?$return['quaninfo']['pcd']:$member['pcd'];
	$return['quaninfo']['pcd'] = json_decode($return['quaninfo']['pcd'],true);
	if($return['quaninfo']['snsid'] && !$return['quaninfo']['pcd']['district']){
		$sns = pdo_get('jy_quan_sns',array('id'=>$return['quaninfo']['snsid'],'uniacid'=>$_SESSION['uniacid']));	
		$sns['pcd']=json_decode($sns['pcd'],true);
		$return['quaninfo']['pcd']['district']=$sns['pcd']['district'];
	}
	$return['sharedata']=array(
		'title'=>$return['quaninfo']['title'],
		'desc'=>$return['quaninfo']['desc']?$return['quaninfo']['desc']:$basic['share_desc'],
		'link'=>custom_url('jy_quan/index//quanid='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true),
		'imgUrl'=>$return['quaninfo']['logo'],
	);
	$return['notices']=pdo_getall('core_account_article',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),'','id desc');
	set_html('title',$return['quaninfo']['title']);
	$return['params']=array('keyword'=>'');
	$return['member']=$member;
	exi($return);
}	
if($_RQ['op']=='getlist'){
	$return['params']=array('keyword'=>'');
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$params['mid']=$_SESSION['uid'];
	if($_RQ['keyword']){
		if(is_numeric($_RQ['keyword'])){
			$params['id']=intval($_RQ['keyword']);
			$return['params']['id']=intval($_RQ['keyword']);
		}else{
			$params['title like']='%'.$_RQ['keyword'].'%';
			$return['params']['title']=$_RQ['keyword'];
		}
	}
	//按点赞数排序作品
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_quan_item',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		$sum=count($list);
		foreach($list as &$item){
			$item['content'] = json_decode($item['content'],true);
			if($item['content']){
				$img3=0;
				$img10=0;
				$video=0;
				foreach($item['content'] as $k=>$cv){
					if($cv['type']==3){
						$img3 +=1;
						if($img3>1){
							unset($item['content'][$k]);
						}
					}
					if($cv['type']==10){
						$img10 +=1;
						if($img10>1){
							unset($item['content'][$k]);
						}
					}
					if($cv['type']==11){
						$video +=1;
						if($video>1){
							unset($item['content'][$k]);
						}
					}
				}
			}
			$item['fileds'] = json_decode($item['fileds'],true);
			$item['fee']=array();
			if(!empty($item['fileds'])){
				foreach($item['fileds'] as &$fv){
					if(!empty($fv)){
						foreach($fv as $f){
							if(!empty($f)){
								if($f['type']==18){
									$item['fee'][]['value']=price($f['data']);
								}
							}
						}
					}
				}
			}
			paixu($item['fee'],'value');
			$item['diyform'] = json_decode($item['diyform'],true);
			$item['onlinetime']=$item['onlinetime']?$item['onlinetime']:$item['createtime']+7*24*3600;
			$item['endstatus']=$item['onlinetime']>time()?1:0;
			$item['onlinetime']=date('Y-m-d H:i:s',$item['onlinetime']);
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['isme']=$item['mid']==$_SESSION['uid'] ? 1 :0;
			$item['typetitle']=$this->itemtypes()[$item['type']];
			$item['member']=pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','avatar','nickname'));
			$item['jielinum']=pdo_count('jy_quan_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']),'id');
			$item['msgnumn']=pdo_count('jy_quan_item_msg',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']),'id');
			$item['jieli']=pdo_getslice('jy_quan_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']),array($pindex,6),$total,'','','createtime desc');
			$item['msg']=pdo_getslice('jy_quan_item_msg',array('checked'=>'1','uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']),array($pindex,6),$total,'','','createtime desc');
			if($item['msg']){
				foreach($item['msg'] as &$m){
					$m['createtime'] = date('Y-m-d H:i',$m['createtime']);
					$m['member'] = pdo_get('core_members',array('id'=>$m['mid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','avatar','nickname'));
				}
			}
			if($item['jieli']){
				$allnum=pdo_count('jy_quan_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']));
				foreach($item['jieli'] as $k=>&$j){
					$j['number'] = $allnum -$k;
					$j['createtime'] = date('Y-m-d H:i',$j['createtime']);
					$j['member'] = pdo_get('core_members',array('id'=>$j['mid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','avatar','nickname'));
					if($j['member'] && $item['fansinfo_set']==1){
						$j['member']['nickname']='********';
					}
				}
			}
			$item['likes'] = pdo_getall('jy_quan_item_like',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']));
			if($item['likes']){
				foreach($item['likes'] as &$liv){
					$liv['member'] = pdo_get('core_members',array('id'=>$liv['mid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
				}
			}
			if($item['endstatus']==1){
				$item['jielongtitle']=$item['status']==0?'审核中':'正在接龙';
			}else{
				$item['jielongtitle']=$item['status']==0?'审核中':'接龙已结束';
			}
			if($item['noshow']==1){
				$item['jielongtitle']='预览中';	
			}
			$item['goods']=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','title','subtitle','image','price','salenum','unit','merchid'));
			$item['goods']['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$item['goods']['merchid'],'uniacid'=>$_SESSION['uniacid']),'merchname');
		}
	}
	$return['list']=$list;
	$return['params']['page'] =$pindex;
	exi($return);
}	
if($_RQ['op']=='setmsg'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
	$postdata=get_postdata();
	$item=pdo_get('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($postdata['pid'])));
	$checked = $basic['msg_shenhe']?0:1;
	$data=array(
		'title'=>trim($postdata['msg']),
		'images'=>ijson_encode($postdata['images']),
		'mid'=>$_SESSION['uid'],
		'pid'=>intval($postdata['pid']),
		'uniacid'=>$_SESSION['uniacid'],
		'createtime'=>time(),
		'checked'=>$checked,
		'type'=>$item['type'],
	);
	pdo_insert('jy_quan_item_msg',$data);
	exi('评论成功！');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('帖子不存在哦！','error');
	}
	
	pdo_delete('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除成功！');

}
if($_RQ['op']=='delmsg'){
	if(!$_RQ['id']){
		exi('帖子不存在哦！','error');
	}
	pdo_delete('jy_quan_item_msg',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除成功！');
}
if($_RQ['op']=='checkedmsg'){
	if(!$_RQ['id']){
		exi('帖子不存在哦！','error');
	}
	pdo_update('jy_quan_item_msg',array('checked'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('显示成功！');
}